package ext.tianma.part.twomaterial.twomaterialInterface;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSON;

import ext.com.core.CoreUtil;
import ext.generic.erp.common.CommonPDMUtil;
import ext.generic.erp.common.CommonUtil;
import ext.tianma.bom.eco.BOMHelp;
import ext.tianma.bom.util.StringUtil;
import ext.tianma.bom.workflow.ReWriteBOMReviewConfirmBusiness;
import ext.tianma.part.forms.TMTypeUtil;
import ext.tianma.part.util.IBAUtil;
import ext.tianma.part.util.updateIBAValues.ClassificationSynchronization;
import ext.tianma.util.IbaAttributeUtil;
import wt.fc.QueryResult;
import wt.fc.collections.WTArrayList;
import wt.part.WTPart;
import wt.part.WTPartHelper;
import wt.part.WTPartMaster;
import wt.part.WTPartUsageLink;
import wt.util.WTException;
import wt.vc.VersionControlHelper;
import wt.vc.config.ConfigException;

/**
 * 用于发送二级物料及二级物料BOM物料
 * 
 * @author laichao_sun
 *
 */
public class TwoMaterialService {
	/**
	 * 用于发送二级物料信息
	 * 
	 * @return
	 */
	public static boolean twoMaterialBean(Object object) {
		try {
			List<WTPart> partList = new WTArrayList();
			ArrayList arraylist = new ArrayList();
			if (object instanceof WTPart) {
				// 传递的部件
				WTPart part = (WTPart) object;
				// 获取集合
				if (part != null) {
					// 获取零部件下所有的子件
					partList = ReWriteBOMReviewConfirmBusiness.getAllSubPartsWithSubstitute(part);
					// x去除胶铁一体方法
					for (WTPart wtPart : partList) {
						if (wtPart.getName().equals("胶铁一体")) {
							// 获取胶铁料号
							IBAUtil iba = new IBAUtil(wtPart);
							String AndTKNumber = iba.getIBAValue("AndTK");
							WTPart TKPart = BOMHelp.getPart(AndTKNumber, "", "");
							String AndJKNumber = iba.getIBAValue("AndJK");
							WTPart jkPart = BOMHelp.getPart(AndJKNumber, "", "");
							arraylist.add(TKPart);
							arraylist.add(jkPart);
						}
					}
					partList.removeAll(arraylist);
					List list = new ArrayList();
					// 判断集合是否为null
					if (partList != null && partList.size() > 0) {
						// 遍历所有子件,取其中的一个部件,取其中的值进行值的设置,然后调用接口
						for (WTPart partChild : partList) {
							if(partChild ==null) {
								continue;
							}
							// 封装单个部件信息
							TwoMaterialBean twoMaterialBean = new TwoMaterialBean();
							// 材料编码twosPartNumber
							twoMaterialBean.setTwoLevelMaterialNumber(
									IbaAttributeUtil.getAttributeValue(partChild, "twosPartNumber"));
							WTPart APLPart = null;
							if (partChild.getName().equals("胶铁一体")) {
								APLPart = partChild;
								String classifictionList = IbaAttributeUtil.getAttributeValue(APLPart,"classifictionList");
								twoMaterialBean.setClassifictionIbaValue(classifictionList);
							} else {
								// APL材料 APLNumber
								String APLNumber = IbaAttributeUtil.getAttributeValue(partChild, "APLNumber");
								APLNumber= APLNumber.replaceAll("\\,","");
								APLPart = BOMHelp.getPart(APLNumber, "", "");
								// 分类属性
								List<HashMap<String, String>> ClassificationList = ClassificationSynchronization
										.getNodeIBAByPart2(APLPart);
								Map<String, String> maps = new HashMap<String, String>();
								for (HashMap<String, String> map : ClassificationList) {
									String displayname = map.get("displayname");
									String value = map.get("value");
									// String key = map.get("key");
									maps.put(displayname, value == null ? "" : value);
								}
								twoMaterialBean.setClassifictionIbaValue(maps.toString());
							}
							// APL材料 --名称
							twoMaterialBean.setTwoLevelMaterialClassifiction(APLPart.getName());
							// 品牌供应商
							twoMaterialBean.setPartSupplier(IbaAttributeUtil.getAttributeValue(APLPart, "suppliers"));
							// 材料型号(供应商物料编码)
							twoMaterialBean
									.setMaterialType(IbaAttributeUtil.getAttributeValue(APLPart, "MaterialType"));
							// 对应一级料号
							twoMaterialBean.setTopPartNumber(part.getNumber());
							// 使用范围
							twoMaterialBean.setUsableRange(IbaAttributeUtil.getAttributeValue(APLPart, "UseRange"));
							// 材料状态
							twoMaterialBean
									.setMaterialState(IbaAttributeUtil.getAttributeValue(APLPart, "MaterialStatus"));
							// 环保等级
							twoMaterialBean
									.setEnvironmentalLevel(IbaAttributeUtil.getAttributeValue(APLPart, "EnvProtLevel"));
							// release产地
							twoMaterialBean.setReleaseSite(IbaAttributeUtil.getAttributeValue(APLPart, "releaseSite"));
							// 版本号
							twoMaterialBean.setRevision(
									VersionControlHelper.getIterationDisplayIdentifier(APLPart).toString());

							// 默认单位defaultUnit
							twoMaterialBean.setDefaultUnit(CommonPDMUtil.getERPMappingUnit(APLPart));
							list.add(twoMaterialBean);
						}

						TwoMaterialSendBean twoMaterialSendBean = new TwoMaterialSendBean();
						twoMaterialSendBean.setServiceInterfaceCode("ESBPLMH0004");
						twoMaterialSendBean.setFromSystemCode("101PLM");
						twoMaterialSendBean.setToInterfaceCode("SC_W0001");
						twoMaterialSendBean.setOuZone("OU_TMSH");
						twoMaterialSendBean.setUuid("1617355496bb588e353e80147eea5f45");
						String time = CommonUtil.getCurrentTime();
						twoMaterialSendBean.setRequestTime(time);
						twoMaterialSendBean.setDataType("JSON");
						TwoMaterialPartsBean twoMaterialPartsBean = new TwoMaterialPartsBean();
						twoMaterialPartsBean.setParts(list);
						twoMaterialSendBean.setData(twoMaterialPartsBean);

						String result2 = JSON.toJSONString(twoMaterialSendBean);
						System.out.println("SLC ==部件的json==>" + result2);
						// url post发布物料信息
						boolean result = TwoMaterialHttpToSOA.sendTwoMaterialBean(result2);
						System.out.println("SLC ==调用接口返回的结果==>" + result);

						return result;

					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 用于发送二级物料Bom信息
	 * 
	 * @return
	 * @throws WTException
	 * @throws ConfigException
	 */
	public static boolean twoMaterialBomBean(Object object, String status) {
		try {
			// bom结构
			TwoMaterialBomBean twoMaterialBomBean = new TwoMaterialBomBean();
			List<WTPart> partList = new WTArrayList();
			// 集合封装部件信息
			List list = new ArrayList();
			if (object instanceof WTPart) {
				// 传递过来的部件
				WTPart part = (WTPart) object;
				// 判断部件是否为RFQ部件,如果是,一级物料编码为空
				if (TMTypeUtil.isRFQPart(part)) {
					// 一级物料编码
					twoMaterialBomBean.setTopPartNumber("");
					// RFQ项目编号
					twoMaterialBomBean.setRFQProductNumber(part.getNumber());
				} else {
					// 一级物料编码
					twoMaterialBomBean.setTopPartNumber(part.getNumber());
					// RFQ项目编号
					twoMaterialBomBean.setRFQProductNumber(IbaAttributeUtil.getAttributeValue(part, "partRFQNumber"));
				}

				// 一级物料版本信息
				twoMaterialBomBean.setRevision(VersionControlHelper.getIterationDisplayIdentifier(part).toString());
				// 传給备料核销平台的标识
				twoMaterialBomBean.setStatus(status);
				// 获取零部件下所有的子件
				partList = ReWriteBOMReviewConfirmBusiness.getAllSubPartsWithSubstitute(part);
				ArrayList arraylist = new ArrayList();
				// x去除胶铁一体方法
				for (WTPart wtPart : partList) {
					if (wtPart.getName().equals("胶铁一体")) {
						// 获取胶铁料号
						IBAUtil iba = new IBAUtil(wtPart);
						String AndTKNumber = iba.getIBAValue("AndTK");
						WTPart TKPart = BOMHelp.getPart(AndTKNumber, "", "");
						String AndJKNumber = iba.getIBAValue("AndJK");
						WTPart jkPart = BOMHelp.getPart(AndJKNumber, "", "");
						arraylist.add(TKPart);
						arraylist.add(jkPart);
					}
				}
				partList.removeAll(arraylist);
				for (WTPart childPart : partList) {
					if(childPart ==null) {
						continue;
					}
					
					// 部件信息
					TwoMaterialBomPartBean twoMaterialBomPartBean = new TwoMaterialBomPartBean();
					// 二级料码
					twoMaterialBomPartBean.setTwoLevelMaterialNumber(IbaAttributeUtil.getAttributeValue(childPart, "twosPartNumber"));
					// 分类代码
					WTPart APLPart = null;
					WTPartUsageLink usageLink = null;
					if (childPart.getName().equals("胶铁一体")) {
						APLPart = childPart;
						String jkNumber = IbaAttributeUtil.getAttributeValue(childPart, "AndJK");
						WTPart jkPart = BOMHelp.getPart(jkNumber, "", "");
						usageLink	= getWTPartUsageLink(part,jkPart);
					} else {
						// APL材料 APLNumber
						String APLNumber = IbaAttributeUtil.getAttributeValue(childPart, "APLNumber");
						APLNumber= APLNumber.replaceAll("\\,","");
						APLPart = BOMHelp.getPart(APLNumber, "", "");
						usageLink	= getWTPartUsageLink(part,childPart);
					}
					
					String classifictionCode =TwoMaterialClassifictionCode.getClassifictionCode(APLPart);
					if(StringUtil.isNullOrEmpty(classifictionCode)) {
						throw new WTException("部件:"+APLPart.getNumber()+"没有分类代码!请检查!");
					}
					twoMaterialBomPartBean.setClassifictionCode(classifictionCode);
					//二级物料中文名称
					twoMaterialBomPartBean.setPartCHName(APLPart.getName());
					/**
					 * 以下属性通过部件的bom关系获取---wt.part.WTPartUsageLink
					 */
					// 用量
					twoMaterialBomPartBean.setCount(PartUsageLinkAttribute.getAttribute(usageLink, "quantity"));
					// 长（mm）
					twoMaterialBomPartBean.setLength(PartUsageLinkAttribute.getAttribute(usageLink, "length"));
					// 宽(mm)
					twoMaterialBomPartBean.setWidth(PartUsageLinkAttribute.getAttribute(usageLink, "widths"));
					// 长宽比例
					twoMaterialBomPartBean.setLwRatio(PartUsageLinkAttribute.getAttribute(usageLink, "lwRatio"));
					// 材料厚度(mm)
					twoMaterialBomPartBean
							.setMaterialThickness(PartUsageLinkAttribute.getAttribute(usageLink, "materialThickness"));
					// 层数
					twoMaterialBomPartBean
							.setLayerNumber(PartUsageLinkAttribute.getAttribute(usageLink, "layerNumber"));
					// 功能用途
					twoMaterialBomPartBean.setPurpose(PartUsageLinkAttribute.getAttribute(usageLink, "purpose"));
					// 形状
					twoMaterialBomPartBean.setShape(PartUsageLinkAttribute.getAttribute(usageLink, "shape"));
					// 单位
					twoMaterialBomPartBean
							.setDefaultUnit(PartUsageLinkAttribute.getAttribute(usageLink, "defaultUnit"));
					//twoMaterialBomPartBean.setDefaultUnit(CommonPDMUtil.getERPMappingUnit(parentPart));
					// 补充信息
					twoMaterialBomPartBean
							.setSupplementaryMsg(PartUsageLinkAttribute.getAttribute(usageLink, "supplementaryMsg"));
					// 表面镀膜工艺
					twoMaterialBomPartBean.setSurfaceCoatingTechnology(
							PartUsageLinkAttribute.getAttribute(usageLink, "surfaceCoatingTechnology"));
					// 钢化工艺
					twoMaterialBomPartBean.setSteelmakingProcess(
							PartUsageLinkAttribute.getAttribute(usageLink, "steelmakingProcess"));
					// 外形工艺
					twoMaterialBomPartBean
							.setShapeTechnology(PartUsageLinkAttribute.getAttribute(usageLink, "shapeTechnology"));
					// 是否抛光
					twoMaterialBomPartBean.setPolishing(PartUsageLinkAttribute.getAttribute(usageLink, "polishing"));
					// 孔数
					twoMaterialBomPartBean.setHole(PartUsageLinkAttribute.getAttribute(usageLink, "hole"));

					list.add(twoMaterialBomPartBean);
				}
				

				twoMaterialBomBean.setBom(list);

				TwoMaterialSendBOMBean twoMaterialSendBOMBean = new TwoMaterialSendBOMBean();
				twoMaterialSendBOMBean.setServiceInterfaceCode("ESBPLMH0005");
				twoMaterialSendBOMBean.setFromSystemCode("101PLM");
				twoMaterialSendBOMBean.setToInterfaceCode("SDPH0002");
				twoMaterialSendBOMBean.setOuZone("OU_TMSH");
				twoMaterialSendBOMBean.setUuid("656ab555e56909e754f0b2c659286069");
				String time = CommonUtil.getCurrentTime();
				twoMaterialSendBOMBean.setRequestTime(time);
				twoMaterialSendBOMBean.setDataType("JSON");
				twoMaterialSendBOMBean.setData(twoMaterialBomBean);

				String result2 = JSON.toJSONString(twoMaterialSendBOMBean);
				System.out.println("SLC ==调用接口的部件BOM结构的json==>" + result2);
				// url post发布物料信息
				boolean result = TwoMaterialHttpToSOA.TwoMaterialBomBean(result2);
				System.out.println("SLC ==调用接口返回的结果==>" + result);

				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	//通过子件获取与父件的使用关系
	public static WTPartUsageLink getWTPartUsageLink(WTPart parPart, WTPart childPart) {
		QueryResult qr = null;
		WTPartUsageLink link = null;
		WTPartMaster master = null;
		if (parPart != null && childPart != null) {
			try {
				qr = WTPartHelper.service.getUsesWTPartMasters(parPart);
				while (qr.hasMoreElements()) {
					link = (WTPartUsageLink) qr.nextElement();
					master = (WTPartMaster) link.getUses();
					if (childPart.getMaster().equals(master)) {
						return link;
					}
				}
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
		return link;
	}
	
}
